
//
//  GKRedPacketViewController.m
//  jinyishe
//
//  Created by xiaoxiaoniuzai on 2017/11/8.
//  Copyright © 2017年 wy. All rights reserved.
//

#import "GKRedPacketViewController.h"
#import "GKRedPacketViewCell.h"
#import "GKLRedSelectModel.h"


@interface GKRedPacketViewController ()

@property (nonatomic, strong) GKLRedSelectModel *selecteRedPacketModel;

@property (nonatomic, strong) UIView *sureView;
@property (nonatomic, weak) GKRedPacketViewCell *selcteCell;
@property (nonatomic, strong) NSMutableArray *redPackets;
@property (nonatomic, strong) NSMutableArray   *firstSectionArray;
@property (nonatomic, strong) NSMutableArray   *secondSectionArray;

@end

@implementation GKRedPacketViewController

- (UIView *)sureView {
    
    if (!_sureView) {
        CGFloat height = 60;
        _sureView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - height - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin, self.view.frame.size.width, height)];
        _sureView.backgroundColor = [UIColor whiteColor];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(12, 8, _sureView.frame.size.width - 24, _sureView.frame.size.height - 16);
        sureBtn.layer.cornerRadius = 4;
        sureBtn.backgroundColor = [UIColor custom_color:@"#CA374F"];
        [sureBtn setTitle:@"确认购买" forState:UIControlStateNormal];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [sureBtn addTarget:self action:@selector(sureBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [_sureView addSubview:sureBtn];
    }
    return _sureView;
}

- (instancetype)initWithUsableRedArray:(NSMutableArray *)usableRedArray redModel:(GKLRedSelectModel *)model
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        self.view.backgroundColor = [UIColor whiteColor];
        self.selecteRedPacketModel = model;
        self.redPackets = usableRedArray;
        [self handleResponseData:usableRedArray];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"红包选择";
     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sun"] isEqualToString:@"wind"]) {//crc
         [self confingureNav];
     }
   // [self.view addSubview:self.sureView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - TabbarSafeBottomMargin);
}

- (void)sureBtnDidClick:(UIButton *)btn {

    if (self.selectedRedPacketBlock) {
        self.selectedRedPacketBlock(self.selecteRedPacketModel);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)confingureTableView {
    [super confingureTableView];
    
    self.tableView.rowHeight = 116;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor custom_color:@"#F7F7F7"];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GKRedPacketViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GKRedPacketViewCell class])];
}



- (void)confingureNav {
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"使用说明" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender {
    NSLog(@"红包说明");
}

- (void)handleResponseData:(NSMutableArray *)array {
    [array enumerateObjectsUsingBlock:^(GKLRedSelectModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (self.selecteRedPacketModel) {
            if ([self.selecteRedPacketModel.id isEqual:obj.id]) {
                [self.firstSectionArray addObject:obj];
            } else {
                [self.secondSectionArray addObject:obj];
            }
        } else {
            [self.secondSectionArray addObject:obj];
        }
    }];
    [self.tableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.firstSectionArray.count > 0) {
        if (self.secondSectionArray.count > 0) {
            return 2;
        } else {
            return 1;
        }
    } else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.firstSectionArray.count > 0) {
            return self.firstSectionArray.count;
        } else {
            return self.secondSectionArray.count;
        }
    } else {
        return self.secondSectionArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GKRedPacketViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GKRedPacketViewCell class])];
    GKLRedSelectModel *redPacketModel;
    if (indexPath.section == 0) {
        if (self.firstSectionArray.count > 0 ) {
            redPacketModel = self.firstSectionArray[indexPath.row];
        } else
        {
            redPacketModel = self.secondSectionArray[indexPath.row];
        }
    } else {
        redPacketModel = self.secondSectionArray[indexPath.row];
    }
    
    cell.redPacketModel = redPacketModel;
    
    if (self.selecteRedPacketModel && [self.selecteRedPacketModel.id isEqual: redPacketModel.id]) {
        [cell setRedPacketSelecteStatu:YES];
        self.selcteCell = cell;
    } else {
        [cell setRedPacketSelecteStatu:NO];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GKRedPacketViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if ([cell isEqual:self.selcteCell]) {
        [self.selcteCell setRedPacketSelecteStatu:NO];
        self.selcteCell = nil;
        self.selecteRedPacketModel = nil;
    } else {
        
        if (self.selcteCell) {
            [self.selcteCell setRedPacketSelecteStatu:NO];
            self.selcteCell = nil;
            self.selecteRedPacketModel = nil;
        }
        
        GKLRedSelectModel *redPacketModel;
        if (indexPath.section == 0) {
            if (self.firstSectionArray.count > 0 ) {
                redPacketModel = self.firstSectionArray[indexPath.row];
            } else
            {
                redPacketModel = self.secondSectionArray[indexPath.row];
            }
        } else {
            redPacketModel = self.secondSectionArray[indexPath.row];
        }
        self.selecteRedPacketModel = redPacketModel;
        self.selcteCell = cell;
        if (self.selectedRedPacketBlock) {
            self.selectedRedPacketBlock(self.selecteRedPacketModel);
        }
        [self.navigationController popViewControllerAnimated:YES];
//        [cell setRedPacketSelecteStatu:YES];
    }
    

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GKRedPacketViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setRedPacketSelecteStatu:NO];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (NSMutableArray *)redPackets
{
    if (!_redPackets) {
        _redPackets = [NSMutableArray array];
    }
    return _redPackets;
}

- (NSMutableArray *)firstSectionArray
{
    if (_firstSectionArray == nil) {
        _firstSectionArray = [NSMutableArray array];
    }
    return _firstSectionArray;
}

- (NSMutableArray *)secondSectionArray
{
    if (_secondSectionArray == nil) {
        _secondSectionArray = [NSMutableArray array];
    }
    return _secondSectionArray;
}

- (UIImage *)tableViewEmptyPlaceHolderImage {
    return [UIImage imageNamed:@"noData"];
}
- (NSAttributedString *)tableViewEmptyTitle {
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:@"没有可用红包" attributes:@{NSForegroundColorAttributeName:[UIColor custom_color:@"#BBBBBB"], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    return attr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
